package com.shiliao.framework.network;

import com.shiliao.framework.YimeiFrameworkAppliction;
import com.shiliao.framework.util.CommonUtil;

import java.io.IOException;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 可以用okhttp的拦截器Interceptor来实现在本地修改响应头的内容,
 * 可以在里面判断wifi,4g,3g来实现不同的缓存策略。
 * Created by yjl on 2017/6/14.
 */

public class MInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        //TODO 如果没有网络，则启用缓存 FORCE_CACHE
        if (!CommonUtil.isNetWorkConnected(YimeiFrameworkAppliction.getInstance())) {
            request = request
                    .newBuilder()
                    .cacheControl(CacheControl.FORCE_CACHE)
                    .build();
        }
        Response originalResponse = chain.proceed(request);
        return originalResponse
                .newBuilder()
                //在这里生成新的响应并修改它的响应头
                .header("Cache-Control", "public,max-age=3600")
                .removeHeader("Pragma").build();
    }

}
